package com.notice.repository;

import com.notice.model.CommentModel;
import com.notice.dao.CommentDao;
import com.notice.entity.Comments;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class CommentRepository {
    
    @Autowired
    private CommentDao commentDao;
    
    public boolean save(CommentModel comment) {
        return commentDao.Add(convertToEntity(comment)) > 0;
    }
    
    public List<CommentModel> find(CommentModel query) {
        return commentDao.FindAll(convertToEntity(query)).stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    public int GetSumVote(CommentModel comment){
        return commentDao.GetSumVote(convertToEntity(comment));
    }
    
    public boolean delete(CommentModel comment) {
        return commentDao.Delete(convertToEntity(comment)) > 0;
    }
    
    public boolean update(CommentModel comment) {
        return commentDao.Update(convertToEntity(comment)) > 0;
    }
    
    private Comments convertToEntity(CommentModel model) {
        if (model == null) return null;
        
        Comments entity = new Comments();
        entity.setCid(model.getCid());
        entity.setCommnet(model.getCommnet());
        System.out.println("=========="+model.getCommnet());
        entity.setUid(model.getUid());
        entity.setMid(model.getMid());
        entity.setUpvote(model.getUpvote());
        return entity;
    }
    
    private CommentModel convertToModel(Comments entity) {
        if (entity == null) return null;
        
        CommentModel model = new CommentModel();
        model.setCid(entity.getCid());
        model.setCommnet(entity.getCommnet());
        model.setUid(entity.getUid());
        model.setMid(entity.getMid());
        model.setUpvote(entity.getUpvote());
        return model;
    }
} 